package net.worldscale.xp.wslib.wstest;

import java.io.PrintStream;

public abstract class TestCase {

    private String mName = "";
    private PrintStream mOut;

    public TestCase() {
    }

    public final void start() {
        doStart(null);
    }

    public final void start(PrintStream ps) {
        doStart(ps);
    }

    private final void doStart(PrintStream ps) {
        try {
            if (ps == null) {
                this.mOut = System.out;
            } else {
                this.mOut = ps;
            }
            main();
        } catch (Throwable e) {
            e.printStackTrace(ps);
        }
    }

    public abstract void main();

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        if (name != null) {
            mName = name;
        }
    }

    public static String timespanToString(long ts) {
        return String.valueOf(ts);
    }

    public static String timestampToString(long ts) {
        return String.valueOf(ts);
    }

    public void printCallStack() {
        try {
            throw new RuntimeException("call stack");
        } catch (Exception e) {
            e.printStackTrace(this.mOut);
        }
    }

    private final void assertEquals() {
        mOut.println("assertEquals(a,b)");
    }

    public void assertEquals(int a, int b) {
        if (a != b) {
            assertEquals();
            mOut.println("    a=" + a);
            mOut.println("    b=" + b);
        }
    }

    public void message(String msg) {
        mOut.println(msg);
    }

    public void assertEquals(String a, String b) {
        if (a == null) {
            if (b == null) {
                return;
            }
        } else {
            if (b != null) {
                if (a.equals(b)) {
                    return;
                }
            }
        }
        assertEquals();
        mOut.println("    a=" + a);
        mOut.println("    b=" + b);
    }

    public PrintStream getOut() {
        return mOut;
    }

    public void fail(String msg) {
        mOut.println("fail");
        mOut.println("    message=" + msg);
    }
}
